// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

// scenario wide flags (used to indicate you know something or not)
// 2, 5 = read note about Avernum passage...
// 2, 15 = gave coins to begger in Cresent Harbor (henner may talk to you...)
// 2, 19 = henner will talk to you now (has mayor clunes note)
// 2, 20 = people in Farthest won't be suspicious of you
// 11, 8 = bandits taken care of
// 231, 0 = know where passage to Avernum is (sortof)...

// quest flags - use one for getting quest, one for completing
//  -- should there be a way to tell if you are on a quest otherwise?
// 50, 0 = Have main quest (autoset when entering the scenario)
// 50, 1 = Completed main quest (done something with she-devil)
// 50, 2 = Have Jane's quest (Cresent Tail)
// 50, 3 = Completed Jane's quest (returned amulet)
// 50, 4 = Have Mayor Clune's quest (Cresent Harbor)
// 50, 5 = Completed Clune's quest (cleaned out Cresent Lighthouse)
// 50, 6 = Have Jennie's quest (Asner)
// 50, 7 = Complete Jennie's quest (returned candlestick)
// 50, 8 = Have Jock's quest (Bowler)
// 11, 8 = Completed Jock's quest (removed bandit threat from region)
// 50, 9 = killed she-devil to complete main quest...
// 50, 10 = Have Terra's first quest (Aries)
// 50, 11 = Completed Terra's first quest
// 50, 12 = Have Terra's second quest (Aries)
// 50, 13 = Completed Terra's second quest

beginscenarioscript;

variables;

short i, daypassed, herb, choice, howmny, sdfx, sdfy, mnlvl, nlvl;

string name, msg;

body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.
beginstate LOAD_SCEN_STATE;
	// quests
	init_quest(0, "Rid she-devil from region", "Ben has pleaded for your help in ridding Cresent Valley of a she-devil that is tormenting the populace.");
	init_quest(1, "Find Jane's Bracelet", "Jane in Cresent Tail asked you to retrieve her bracelet by a rock south of the end of the west road.");
	init_quest(2, "Clear out Lighthouse", "Mayor Clune in Cresent Harbor needs the Cresent Lighthouse cleaned out of the undead that have taken it over.");
	init_quest(3, "Find Candlestick", "Jennie in Asner wants her candlestick back that had been stolen from her.");
	init_quest(4, "Clean out Bandits", "Jock in Bowler wants the party to find and destroy  the bandits preying on the Cresent Bowl region of Cresent Valley.");
	init_quest(5, "Deliver Potion", "Terra in Aries needs you to deliver a potion to a sick farmer east of Aries.");
	init_quest(6, "Find herb", "Terra needs you to find a sprig of parsley she needs to create a potion.");

	// special items
	init_special_item(1, "Heavy Iron Key", "You found the heavy key in a bookcase.  It fits a lock somewhere.");
	init_special_item(2, "Jeweled Bracelet", "A thick gold bracelet with diamonds around the edge was found by a rock outside of Cresent Tail.");
	init_special_item(3, "Lighthouse Key", "You have a large steel key that will unlock the entrance to Cresent Lighthouse.");
	init_special_item(4, "Golden Candlestick", "The heavy golden candle stick is stamped at the base with the name 'Jennie'.");
	init_special_item(5, "Small Devil Doll", "The little, worn, devil doll has a spring loaded arm and removable pitchfork.");
	init_special_item(6, "Black Key", "The cold, black key opens the watchers building in Farthest.");
	init_special_item(7, "Warm Potion", "The warm potion will help cure what ails a sick farmer in Avernum.");
	init_special_item(8, "Parsley Sprig", "The fresh sprig of parsley seems just what is needed to create potions with.");
break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
beginstate START_SCEN_STATE;
	// balance up some of the monsters in the scenario
	mnlvl = 200;
	i = 0;
	// get smallest/biggest level in party
	while (i < 4) {
		nlvl = get_level(i);
		if (nlvl < mnlvl)
			mnlvl = nlvl;
		i = i + 1;
	}
	if (mnlvl > 20) {
		// most of these are the wandering monsters
		// -- add 5 to levels if min is > 20
		// hell creatures
		set_creature_type_level(57, 12);
		set_creature_type_level(59, 14);
		// rats
		set_creature_type_level(72, 13);
		set_creature_type_level(176, 25);
		// undead
		set_creature_type_level(120, 10);
		set_creature_type_level(121, 15);
		// bump up rouge xxx
		set_creature_type_level(123, 17);
		set_creature_type_level(124, 17);
		set_creature_type_level(125, 17);
		// unicorn ++
            set_creature_type_level(231, 8);
		// bump up bandits (custom)...
		set_creature_type_level(242, 25);
		set_creature_type_level(243, 25);
		set_creature_type_level(244, 25);
		set_creature_type_level(245, 35);
	}
	else if (mnlvl > 15) {
		set_creature_type_level(57, 9);
		set_creature_type_level(59, 11);
		set_creature_type_level(72, 10);
		set_creature_type_level(120, 7);
		set_creature_type_level(121, 12);
		set_creature_type_level(176, 22);
            set_creature_type_level(231, 5);
	}

	// set up the horses available in Cresent Harbor
	create_horse(0, 2, 8, 7, 1);
	create_horse(1, 2, 12, 8, 1);
	create_horse(2, 2, 10, 6, 1);

	// Initialize a few shops.
	
	// Shop 0 - armor / Arena
	add_item_to_shop(0, 27, 5);
	add_item_to_shop(0, 32, 5);
	add_item_to_shop(0, 37, 5);
	add_item_to_shop(0, 123, 5);
	add_item_to_shop(0, 128, 5);
	add_item_to_shop(0, 133, 5);
	add_item_to_shop(0, 143, 5);
	add_item_to_shop(0, 148, 5);
	
	// Shop 1 - weapons / Arena
	add_item_to_shop(1, 47, 3);
	add_item_to_shop(1, 52, 3);
	add_item_to_shop(1, 57, 3);
	add_item_to_shop(1, 62, 2);
	add_item_to_shop(1, 67, 3);
	add_item_to_shop(1, 72, 3);
	add_item_to_shop(1, 77, 2);
	add_item_to_shop(1, 87, 3);
	
	// Shop 2 - bows / Arena
	add_item_to_shop(2, 92, 2);
	add_item_to_shop(2, 97, 2);
	add_item_to_shop(2, 102, 100);
	add_item_to_shop(2, 107, 100);
	
	// Shop 3 - magic shop / Arena
	add_item_to_shop(3, 220, 3);
	add_item_to_shop(3, 221, 3);
	add_item_to_shop(3, 223, 3);
	add_item_to_shop(3, 228, 3);
	add_item_to_shop(3, 229, 3);
	add_item_to_shop(3, 231, 3);

	// Shop 4 - misc supplies / Arena
	add_item_to_shop(4, 248, 3);
	add_item_to_shop(4, 270, 3);
	add_item_to_shop(4, 178, 3);
	add_item_to_shop(4, 175, 3);
	add_item_to_shop(4, 138, 3);
	add_item_to_shop(4, 326, 3);

	// Shop 5 - food / Arena
	add_item_to_shop(5, 4, 500);
	add_item_to_shop(5, 6, 500);
	add_item_to_shop(5, 7, 500);
	add_item_to_shop(5, 8, 500);

	// shop 6 - blacksmith / Cresent Entry
	add_item_to_shop(6, 27, 2);
	add_item_to_shop(6, 37, 2);
	add_item_to_shop(6, 52, 2);
	add_item_to_shop(6, 57, 2);
	add_item_to_shop(6, 91, 2);
	add_item_to_shop(6, 96, 2);
	add_item_to_shop(6, 102, 100);
	add_item_to_shop(6, 107, 100);
	add_item_to_shop(6, 123, 2);

	// shop 7 - priest spells / Arena
	add_item_to_shop(7, 3000, 6);
	add_item_to_shop(7, 3001, 6);
	add_item_to_shop(7, 3002, 6);
	add_item_to_shop(7, 3003, 5);
	add_item_to_shop(7, 3004, 5);
	add_item_to_shop(7, 3005, 5);
	add_item_to_shop(7, 3006, 4);
	add_item_to_shop(7, 3007, 4);
	add_item_to_shop(7, 3008, 4);
	add_item_to_shop(7, 3009, 3);
	add_item_to_shop(7, 3010, 3);
	add_item_to_shop(7, 3011, 3);
	add_item_to_shop(7, 3012, 3);
	add_item_to_shop(7, 3013, 2);
	add_item_to_shop(7, 3014, 2);
	add_item_to_shop(7, 3015, 2);
	add_item_to_shop(7, 3016, 2);

	// Shop 8 - Pete's, Cresent Tail
	add_item_to_shop(8, 5, 500);
	add_item_to_shop(8, 11, 500);

	// Shop 9 - Clairesse Magic, Cresent Harbor
	add_item_to_shop(9, 2000, 6);
	add_item_to_shop(9, 2001, 6);
	add_item_to_shop(9, 2002, 6);
	add_item_to_shop(9, 2003, 5);
	add_item_to_shop(9, 2004, 5);
	add_item_to_shop(9, 2005, 5);
	add_item_to_shop(9, 2006, 4);
	add_item_to_shop(9, 2007, 4);
	add_item_to_shop(9, 2008, 4);
	add_item_to_shop(9, 2009, 3);
	add_item_to_shop(9, 2010, 3);
	add_item_to_shop(9, 2011, 3);
	add_item_to_shop(9, 2012, 3);
	add_item_to_shop(9, 2013, 2);
	add_item_to_shop(9, 2014, 2);
	add_item_to_shop(9, 2015, 2);
	add_item_to_shop(9, 2016, 2);	

	// shop 10 - Stan, Cresent Harbor
	add_item_to_shop(10, 166, 500);
	add_item_to_shop(10, 414, 500);
	add_item_to_shop(10, 415, 500);

	// shop 11 - Clay, Cresent Harbor
	add_item_to_shop(11, 27, 3);
	add_item_to_shop(11, 37, 3);
	add_item_to_shop(11, 57, 3);
	add_item_to_shop(11, 67, 3);
	add_item_to_shop(11, 77, 3);
	add_item_to_shop(11, 114, 3);
	add_item_to_shop(11, 128, 3);
	add_item_to_shop(11, get_ran(1, 146, 148), 2);
	if (get_ran(1, 1, 100) > 80)
		add_item_to_shop(11, 176, 2);
	else
		add_item_to_shop(11, 175, 10);
	add_item_to_shop(11, 178, 10);
	add_item_to_shop(11, 297, 1);
	add_item_to_shop(11, 298, 1);
	add_item_to_shop(11, 299, 1);
	add_item_to_shop(11, get_ran(1, 321, 324), 2);

	// shop 12 - Wanda's Herbs, Asner
	add_item_to_shop(12, 214, 10);
	add_item_to_shop(12, 215, 10);
	add_item_to_shop(12, 216, 10);
	add_item_to_shop(12, 217, 5);
	add_item_to_shop(12, 218, 10);
	add_item_to_shop(12, 219, 5);

	// shop 13 - Carrie's Potions, Asner
	add_item_to_shop(13, 220, 5);
	add_item_to_shop(13, 221, 5);
	add_item_to_shop(13, 223, 5);
	add_item_to_shop(13, 225, 5);
	add_item_to_shop(13, 226, 3);
	add_item_to_shop(13, 227, 3);
	add_item_to_shop(13, 228, 2);
	add_item_to_shop(13, 229, 2);
	add_item_to_shop(13, 231, 2);
	add_item_to_shop(13, 233, 2);
	add_item_to_shop(13, 235, 2);
	add_item_to_shop(13, 237, 1);
	add_item_to_shop(13, 238, 1);

	// Shop 14 - food / Bowler
	add_item_to_shop(14, 4, 500);
	add_item_to_shop(14, 6, 500);
	add_item_to_shop(14, 397, 500);
	add_item_to_shop(14, 398, 500);
	add_item_to_shop(14, 399, 5);

	// shop 15 - magic / Farthest
 	add_item_to_shop(15, 124, 1);
 	add_item_to_shop(15, 147, 1);
 	add_item_to_shop(15, 176, 3);
 	add_item_to_shop(15, 251, 2);
 	add_item_to_shop(15, 254, 2);
 	add_item_to_shop(15, 297, 1);
 	add_item_to_shop(15, 325, 2);
 	add_item_to_shop(15, 417, 3);

	// shop 16 - general / Farthest
 	add_item_to_shop(16, 18, 3);
 	add_item_to_shop(16, 114, 3);
 	add_item_to_shop(16, 178, 3);
 	add_item_to_shop(16, 249, 1);
 	add_item_to_shop(16, 293, 1);
 	add_item_to_shop(16, 298, 1);
 	add_item_to_shop(16, 318, 2);
 	add_item_to_shop(16, 319, 2);

	// shop 17 - swords-armor / Farthest
 	add_item_to_shop(17, 38, 1);
 	add_item_to_shop(17, 68, 1);
 	add_item_to_shop(17, 115, 1);
 	add_item_to_shop(17, 139, 1);
 	add_item_to_shop(17, 299, 1);
 	add_item_to_shop(17, 324, 2);
 	add_item_to_shop(17, 352, 10);
 	add_item_to_shop(17, 353, 10);
 	add_item_to_shop(17, 354, 10);
 	add_item_to_shop(17, 355, 10);
 	add_item_to_shop(17, 384, 1);

	// shop 18 - tools / Farthest
 	add_item_to_shop(18, 148, 1);
 	add_item_to_shop(18, get_ran(1, 274, 278), 1);
 	add_item_to_shop(18, get_ran(1, 322, 323), 1);
 	add_item_to_shop(18, 331, 1);
	if (get_ran(1, 1, 100) > 50)
 		add_item_to_shop(18, 337, 1);
 	add_item_to_shop(18, 342, 1);
	add_item_to_shop(18, 377, 1);
	add_item_to_shop(18, 406, 1);
	add_item_to_shop(18, 409, 1);
 	add_item_to_shop(18, 411, 1);
 	add_item_to_shop(18, 412, 5);

	// shop 19 - vahnatai trove / Aires
	add_item_to_shop(19, 329, 3);
	add_item_to_shop(19, 380, 2);
	add_item_to_shop(19, 381, 1);
	add_item_to_shop(19, 384, 2);
	add_item_to_shop(19, 385, 2);
	add_item_to_shop(19, 387, 3);
	add_item_to_shop(19, 409, 2);

	// shop 20 - clothes / Aires
	add_item_to_shop(20, 13, 3);
	add_item_to_shop(20, 14, 3);
	add_item_to_shop(20, 15, 3);
	add_item_to_shop(20, 17, 3);
	add_item_to_shop(20, 19, 3);
	add_item_to_shop(20, 20, 3);
	add_item_to_shop(20, 22, 3);
	add_item_to_shop(20, 23, 3);
	add_item_to_shop(20, 111, 3);
	add_item_to_shop(20, 114, 3);
	add_item_to_shop(20, 332, 1);
	add_item_to_shop(20, 333, 1);
	add_item_to_shop(20, 335, 1);

	// shop 21 - forge / Aires
	add_item_to_shop(21, 47, 2);
	add_item_to_shop(21, 52, 2);
	add_item_to_shop(21, 57, 2);
	add_item_to_shop(21, 72, 2);
	add_item_to_shop(21, 123, 2);
	add_item_to_shop(21, 128, 2);
	add_item_to_shop(21, 380, 2);

	// shop 22 - misc / Aires
	add_item_to_shop(22, get_ran(1, 26, 28), 1);
	add_item_to_shop(22, get_ran(1, 137, 139), 1);
	add_item_to_shop(22, get_ran(1, 174, 176), 3);
	add_item_to_shop(22, get_ran(1, 236, 241), 2);
	add_item_to_shop(22, get_ran(1, 304, 310), 1);
	add_item_to_shop(22, get_ran(1, 304, 310), 1);
	add_item_to_shop(22, get_ran(1, 321, 325), 1);
	add_item_to_shop(22, get_ran(1, 326, 329), 3);
break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;
	// reset herbs
	daypassed = get_current_tick() % 5000;
	if (daypassed == 0) {
		print_str_color("A new day is starting.", 3);
		if (get_flag(6, 3) > 0)
			inc_flag(6, 3, -1);
		if (get_flag(6, 4) > 0)
			inc_flag(6, 4, -1);
		if (get_flag(6, 5) > 0)
			inc_flag(6, 5, -1);
		if (get_flag(8, 3) > 0)
			inc_flag(8, 3, -1);
		if (get_flag(201, 0) > 0)
			inc_flag(201, 0, -1);
		if (get_flag(202, 0) > 0)
			inc_flag(202, 0, -1);
		if (get_flag(210, 0) > 0)
			inc_flag(210, 0, -1);
		if (get_flag(212, 2) > 0)
			inc_flag(212, 2, -1);
		if (get_flag(212, 3) > 0)
			inc_flag(212, 3, -1);
		if (get_flag(221, 1) > 0)
			inc_flag(221, 1, -1);
		if (get_flag(222, 0) > 0)
			inc_flag(222, 0, -1);
		if (get_flag(232, 2) > 0)
			inc_flag(232, 2, -1);
		if (get_flag(232, 3) > 0)
			inc_flag(232, 3, -1);
	}
break;

// Place your own states below. Give each a number at least 10.
beginstate 10;
	// use Avernum Enterence Map
	small_draw_pic_dialog(560, "The old map seems to show an entrance to Avernum.");
break;

beginstate 11;
	// use heal all
	message_dialog("You use the 'heal all' device.", "A warm glow encircles each member of the group.  You all feel completely healed.");
	i = 0;
	while (i < 6) {
		put_sparkles_on_char(i, 0, 6);
		i = i + 1;
	}
	run_animation_sound(60);
	revive_party();
break;

beginstate 12;
	if (get_sdf(sdfx, sdfy) == 0) {
		clear_buffer();
		append_string("There is a little patch of ");
		append_string(name);
		append_string(" here.");
		get_buffer_text(msg);
		reset_dialog();
		add_dialog_str(0, msg, 0);
		add_dialog_str(1, "Do you collect it?", 0);
		add_dialog_choice(0, "Leave Alone");
		add_dialog_choice(1, "Collect");
		choice = run_dialog(0);
		if (choice == 2) {
			set_flag(sdfx, sdfy, get_ran(1, 2, 5));
			i = 0;
			howmny = get_ran(1, 2, 5);
			while (i < howmny) {
				if (reward_give(herb) == 0) {
					// reset flag - didn't pick completly - no room
					set_flag(sdfx, sdfy, 0);
					end();
				}
				i = i + 1;
			}
			clear_buffer();
			append_string("The party collect some ");
			append_string(name);
			append_string(".");
			get_buffer_text(msg);
			print_str_color(msg, 4);
		}

	}
	else {
		clear_buffer();
		append_string("The ");
		append_string(name);
		if ((herb == 217) || (herb == 219))
			append_string(" hasn't ");
		else
			append_string(" haven't ");
		append_string("grown back yet.");
		get_buffer_text(msg);
		print_str_color(msg, 4);
	}
break;

beginstate 13;
	clear_buffer();
	append_string("Toadstools");
	get_buffer_text(name);
	herb = 218;
	sdfx = 232;
	sdfy = 2;
	set_state_continue(12);
break;

beginstate 14;
	clear_buffer();
	append_string("Energetic Herbs");
	get_buffer_text(name);
	herb = 216;
	sdfx = 232;
	sdfy = 3;
	set_state_continue(12);
break;

beginstate 15;
	clear_buffer();
	append_string("Healing Herbs");
	get_buffer_text(name);
	herb = 214;
	sdfx = 222;
	sdfy = 0;
	set_state_continue(12);
break;

beginstate 16;
	clear_buffer();
	append_string("Graymold");
	get_buffer_text(name);
	herb = 217;
	sdfx = 212;
	sdfy = 2;
	set_state_continue(12);
break;

beginstate 17;
	clear_buffer();
	append_string("Healing Herbs");
	get_buffer_text(name);
	herb = 214;
	sdfx = 212;
	sdfy = 3;
	set_state_continue(12);
break;

beginstate 18;
	clear_buffer();
	append_string("Spiritual Herbs");
	get_buffer_text(name);
	herb = 215;
	sdfx = 202;
	sdfy = 0;
	set_state_continue(12);
break;

beginstate 19;
	clear_buffer();
	append_string("Mandrake");
	get_buffer_text(name);
	herb = 219;
	sdfx = 201;
	sdfy = 0;
	set_state_continue(12);
break;

beginstate 20;
	clear_buffer();
	append_string("Energetic Herbs");
	get_buffer_text(name);
	herb = 216;
	sdfx = 210;
	sdfy = 0;
	set_state_continue(12);
break;

beginstate 21;
	clear_buffer();
	append_string("Toadstools");
	get_buffer_text(name);
	herb = 218;
	sdfx = 221;
	sdfy = 1;
	set_state_continue(12);
break;
